
const Goods = require("../model/goods.model")

/*
 * @Descripttion: 商品相关数据库操作
 * @version: 
 * @Author: 一氧化二氢
 * @Date: 2022-01-03 01:02:51
 * @lastEditorTime: Do no edit
 */
class GoodService {
    /**
     * @name: 
     * @test: test font
     * @msg: 在数据库中创建商品
     * @return {*}
     * @param {*} goods
     */
    async createGoods(goods) {
        const res = await Goods.create(goods)
        return res.dataValues
    }

    /**
     * @name: 
     * @test: test font
     * @msg: 在数据库中更新商品
     * @return {*}
     * @param {*} id
     * @param {*} goods
     */
    async updateGoods(id, goods) {
        const res = await Goods.update(goods, { where: { id } })
        return res[0] > 0 ? true : false

    }

    /**
     * @name: 
     * @test: test font
     * @msg: 下架商品
     * @return {*}
     * @param {*} id
     */
    async removeGoods(id) {
        const res = await Goods.destroy({ where: { id } })
        console.log(res)
        return res
    }

    /**
     * @name: 
     * @test: test font
     * @msg: 上架商品
     * @return {*}
     * @param {*} id
     */
    async restoreGoods(id) {
        const res = await Goods.restore({ where: { id } })
        return res
    }

    /**
     * @name: 
     * @test: test font
     * @msg: 从数据库中获取商品列表
     * @return {*}
     * @param {*} pageNum
     * @param {*} pageSize
     */
    async findGoods(pageNum, pageSize) {
        const offSet = (pageNum - 1) * pageSize
        const res = await Goods.findAndCountAll({ offSet: offSet, limit: offSet * 1 })
        const { count, rows } = await Goods.findAndCountAll({ offSet: offSet, limit: pageSize * 1 })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }

}
module.exports = new GoodService()
